﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class movement : MonoBehaviour
{
	Rigidbody play_body;
	public float acc_speed = 10;
	public float max_speed = 30;
	
	Vector3 desired_speed = new Vector3(0,0,0);
	Vector3 velocity = new Vector3(0,0,0);

	private int UP = 0;//角色状态向前
	private int RIGHT = 1;//角色状态向右
	private int DOWN = 2;//角色状态向后
	private int LEFT = 3;//角色状态向左


	private void Awake()
	{
		play_body = GetComponent<Rigidbody>();
	}
	// Start is called before the first frame update


	void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
		if (Input.GetKey("w"))
		{
			get_accelerate(UP);
		}
		else if (Input.GetKey("s"))
		{
			get_accelerate(DOWN);
		}
		if (Input.GetKey("a"))
		{
			get_accelerate(LEFT);
		}
		else if (Input.GetKey("d"))
		{
			get_accelerate(RIGHT);
		}
	}

	void get_accelerate(int currState)
    {
		switch (currState)
		{
			case 0://角色状态向前时，角色不断向前缓慢移动
				desired_speed = Vector3.forward * max_speed;
				break;
			case 1://角色状态向右时。角色不断向右缓慢移动
				   //transformValue = Vector3.right * Time.deltaTime * speed;
				desired_speed = Vector3.right  * max_speed;
				break;
			case 2://角色状态向后时。角色不断向后缓慢移动
				desired_speed = Vector3.back  * max_speed;
				break;
			case 3://角色状态向左时，角色不断向左缓慢移动
				   //transformValue = Vector3.left * Time.deltaTime * speed;
				desired_speed = Vector3.left * max_speed;
				break;
		}
		velocity = play_body.velocity;
		float max_speed_change = acc_speed * Time.deltaTime;
		velocity.x = Mathf.MoveTowards(velocity.x, desired_speed.x, max_speed_change);
		velocity.z = Mathf.MoveTowards(velocity.z, desired_speed.z, max_speed_change);
		play_body.velocity = velocity;
	}
    private void FixedUpdate()
    {

	}

}

